<?php
function domain($host) {
	if (! strcmp ( long2ip ( sprintf ( "%u", ip2long ( $host ) ) ), $host )) {
		$domain = $host;
		return $domain;
	}
	
	$arr = explode ( ".", $host );
	$count = count ( $arr );
	if (2 === $count) {
		$domain = $host;
	} elseif ($count >= 3) {
		
		if ($arr [0] === "www" && $count === 3) {
			$domain = substr ( $host, 4 );
			return $domain;
		}
		
		$gTLD = array (
				"com",
				"net",
				"org",
				"gov",
				"cc",
				"biz",
				"info",
				"mobi",
				"asia",
				"name",
				"me",
				"tv",
				"travel",
				"edu",
				"az",
				"cx" 
		);
		$end_1 = $arr [$count - 1];
		$end_2 = $arr [$count - 2];
		if (in_array ( $end_1, $gTLD )) {
			$domain = $end_2 . "." . $end_1;
		} elseif ("cn" === $end_1) {
			$cn = array (
					"ac",
					"ah",
					"bj",
					"com",
					"cq",
					"edu",
					"fj",
					"gd",
					"gov",
					"gs",
					"gx",
					"gz",
					"ha",
					"hb",
					"he",
					"hi",
					"hk",
					"hl",
					"hn",
					"jl",
					"js",
					"jx",
					"ln",
					"mo",
					"net",
					"nm",
					"nx",
					"org",
					"qh",
					"sc",
					"sd",
					"sh",
					"sn",
					"sx",
					"tj",
					"tw",
					"xj",
					"xz",
					"yn",
					"zj" 
			);
			if (in_array ( $end_2, $cn )) {
				$domain = $arr [$count - 3] . "." . $arr [$count - 2] . "." . $arr [$count - 1];
			} else {
				$domain = $arr [$count - 2] . "." . $arr [$count - 1];
			}
		} elseif ("hk" === $end_1) {
			$cn = array (
					"com",
					"net",
					"org",
					'gov',
					'edu' 
			);
			if (in_array ( $end_2, $cn )) {
				$domain = $arr [$count - 3] . "." . $arr [$count - 2] . "." . $arr [$count - 1];
			} else {
				$domain = $arr [$count - 2] . "." . $arr [$count - 1];
			}
		} elseif ("tw" === $end_1) {
			$cn = array (
					"club",
					"com",
					"ebiz",
					"edu",
					"game",
					"gov",
					"idv",
					"mil",
					"net",
					"org" 
			);
			if (in_array ( $end_2, $cn )) {
				$domain = $arr [$count - 3] . "." . $arr [$count - 2] . "." . $arr [$count - 1];
			} else {
				$domain = $arr [$count - 2] . "." . $arr [$count - 1];
			}
		} elseif ("jp" === $end_1) {
			$cn = array (
					"ac",
					"ad",
					"co",
					"ed",
					"go",
					"gr",
					"lg",
					"ne",
					"or" 
			);
			if (in_array ( $arr [$count - 2], $cn )) {
				$domain = $arr [$count - 3] . "." . $arr [$count - 2] . "." . $arr [$count - 1];
			} else {
				$domain = $arr [$count - 2] . "." . $arr [$count - 1];
			}
		} elseif ("co" === $end_1) {
			$cn = array (
					"com",
					"edu",
					"org",
					'gov',
					'mil' 
			);
			if (in_array ( $arr [$count - 2], $cn )) {
				$domain = $arr [$count - 3] . "." . $arr [$count - 2] . "." . $arr [$count - 1];
			} else {
				$domain = $arr [$count - 2] . "." . $arr [$count - 1];
			}
		} elseif ("uk" === $end_1) {
			$cn = array (
					"co",
					"ltd",
					"me",
					"net",
					"nic",
					"org",
					"plc",
					"sch" 
			);
			if (in_array ( $arr [$count - 2], $cn )) {
				$domain = $arr [$count - 3] . "." . $arr [$count - 2] . "." . $arr [$count - 1];
			} else {
				$domain = $arr [$count - 2] . "." . $arr [$count - 1];
			}
		} else {
			$cn = array (
					"com",
					"net",
					"org",
					'gov',
					'edu' 
			);
			if (in_array ( $arr [$count - 2], $cn )) {
				$domain = $arr [$count - 3] . "." . $arr [$count - 2] . "." . $arr [$count - 1];
			} else {
				$domain = "";
			}
		}
	} else {
		$domain = '';
	}
	
	return $domain;
}
function get_host($url) {
	$parts = @parse_url ( $url );
	return $parts ["host"];
}